<?php

namespace addons\yungift\model;

use addons\yungift\library\ModelTrait;
use think\Model;
use traits\model\SoftDelete;

/**
 * 礼币 - 记录
 */
class FundRecord extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yungift_fund_record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    // 自定义初始化
    protected static function init()
    {
        GiftSpec::event('before_write', function ($item) {
            return true;
        });
    }

    // 活动数据
    public function activityData()
    {
        return $this->belongsTo(Activity::class, 'activity_id', 'id');
    }

    // 成员数据
    public function memberData()
    {
        return $this->belongsTo(Member::class, 'member_id', 'id');
    }

    // 行为
    const ACTION_MANUAL_PLUS   = 'manual_plus';
    const ACTION_MANUAL_MINUS  = 'manual_minus';
    const ACTION_BATCH_INIT    = 'batch_init';
    const ACTION_BATCH_PLUS    = 'batch_plus';
    const ACTION_BATCH_MINUS   = 'batch_minus';
    const ACTION_FIRST_LOGIN   = 'first_login';
    const ACTION_GIFT_EXCHANGE = 'gift_exchange';

    const ACTION_SYSTEM_ROLLBACK = 'system_rollback';
    const ACTION_GIFT_BACK       = 'gift_back';


    public static function getActionList()
    {
        return [
            self::ACTION_MANUAL_PLUS   => '手动增加',
            self::ACTION_MANUAL_MINUS  => '手动减少',
            self::ACTION_BATCH_INIT    => '初始发放',
            self::ACTION_BATCH_PLUS    => '批量增加',
            self::ACTION_BATCH_MINUS   => '批量减少',
            self::ACTION_FIRST_LOGIN   => '首次登录',
            self::ACTION_GIFT_EXCHANGE => '礼品兑换',
            self::ACTION_GIFT_BACK     => '礼品退回',

            self::ACTION_SYSTEM_ROLLBACK => '系统扣除',
        ];
    }

    public function getActionTextAttr($value, $data)
    {
        $optionList = self::getActionList();

        return $optionList[$data['action']] ?? $data['action'];
    }
}
